/*
 * @Author: linjie linjie@gysk.com.cn
 * @Date: 2023-07-06 11:37:18
 * @LastEditors: linjie linjie@gysk.com.cn
 * @LastEditTime: 2024-05-09 14:38:14
 * @FilePath: \esale-pc-frontend\src\utils\auth.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { setCookie, getCookie, delCookie } from '@/utils/util'

/**
 * 将用户信息存储cookie中
 */
export function setUserCookie (data) {
  for (let index in data) {
    if (index === 'token') {
      setCookie(`user${index}`, data[index])
    } else {
      setCookie(index, data[index])
    }
  }
}
/**
 * 获取cookie中用户信息
 */
export function getUserCookie () {
  const token = getCookie('usertoken')
  const userName = getCookie('userName')
  const tenantId = getCookie('tenantId')
  const realName = getCookie('realName')
  const phone = getCookie('phone')
  const showResetTips = getCookie('showResetTips')
  const passwordExireTime = getCookie('passwordExireTime')
  const loginModel = getCookie('loginModel')
  return { token,realName,phone, userName, tenantId, showResetTips, passwordExireTime, loginModel }
}
/**
 * 删除cookie中用户信息
 */
export function deleteUserCookie () {
  delCookie('usertoken')
  delCookie('userName')
  delCookie('showResetTips')
  delCookie('passwordExireTime')
  delCookie('loginModel')
}

/**
   * 统一登录
   */
export function unifiedLogin () {
  const tenantId = getCookie('tenantId')
  // const loginUrl = process.env.NODE_ENV !== 'prodMaster' ? process.env.LOGIN_URL : `http://${window.location.hostname}:7621/authority/login`
  deleteUserCookie()
  location.href = location.origin + '/agent/login?tenantId=' + tenantId
}

// 权限树展开
export function parsePlainPermission (result, permissions) {
  if (!permissions) {
    return
  }
  permissions.forEach(p => {
    result.push(p.permissionCode)
    parsePlainPermission(result, p.childList)
  })
}
/**
   * 获取当前用户信息
   * @returns {userName,token,permissionIds}
   */
export const getUser = () => {
  let permissionIds = JSON.parse(window.localStorage.getItem('permissionIds'))
  return {
    permissionIds
  }
}
/**
   * 获取当前用户菜单权限
   * @returns {userName,token,permissionIds}
   */
export const getUserPermiss = () => {
  let permissionCodes = JSON.parse(window.localStorage.getItem('permissionCodes'))
  return {
    permissionCodes
  }
}
